package net.lab1024.sa.user.module.business.points.service;

import java.util.List;

import net.lab1024.sa.common.common.util.SmartRequestUtil;
import net.lab1024.sa.user.module.business.points.constant.PointsChangeStatus;
import net.lab1024.sa.user.module.business.points.dao.PointsChangeRecordDao;
import net.lab1024.sa.user.module.business.points.domain.entity.PointsChangeRecordEntity;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordAddForm;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordQueryForm;
import net.lab1024.sa.user.module.business.points.domain.form.PointsChangeRecordUpdateForm;
import net.lab1024.sa.user.module.business.points.domain.vo.PointsChangeRecordVO;
import net.lab1024.sa.common.common.util.SmartBeanUtil;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 积分变更记录 Service
 *
 * @Author jabari
 * @Date 2023-07-12 14:28:55
 * @Copyright 51dev
 */

@Service
public class PointsChangeRecordService {

    @Autowired
    private PointsChangeRecordDao pointsChangeRecordDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<PointsChangeRecordVO> queryPage(PointsChangeRecordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PointsChangeRecordVO> list = pointsChangeRecordDao.queryPage(page, queryForm);
        PageResult<PointsChangeRecordVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(PointsChangeRecordAddForm addForm) {
        PointsChangeRecordEntity pointsChangeRecordEntity = SmartBeanUtil.copy(addForm, PointsChangeRecordEntity.class);
        pointsChangeRecordDao.insert(pointsChangeRecordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(PointsChangeRecordUpdateForm updateForm) {
        PointsChangeRecordEntity pointsChangeRecordEntity = SmartBeanUtil.copy(updateForm, PointsChangeRecordEntity.class);
        pointsChangeRecordDao.updateById(pointsChangeRecordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        pointsChangeRecordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer recordId) {
        if (null == recordId){
            return ResponseDTO.ok();
        }

        pointsChangeRecordDao.deleteById(recordId);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> checkOrderId(Long orderId){

        PointsChangeRecordQueryForm pointsChangeRecordQueryForm=new PointsChangeRecordQueryForm();
        pointsChangeRecordQueryForm.setUserId(SmartRequestUtil.getRequestUserId());
        pointsChangeRecordQueryForm.setChangeStatus(new Integer[]{PointsChangeStatus.APPROVED.getValue(),PointsChangeStatus.COMPLETED.getValue(),PointsChangeStatus.PENDING_APPROVAL.getValue()});
        pointsChangeRecordQueryForm.setOrderId(orderId);
        pointsChangeRecordQueryForm.setPageNum(1);
        pointsChangeRecordQueryForm.setPageSize(5);
        PageResult<PointsChangeRecordVO> pointsChangeRecordVOPageResult = this.queryPage(pointsChangeRecordQueryForm);
        if(!pointsChangeRecordVOPageResult.getEmptyFlag()){
            return ResponseDTO.userErrorParam("订单已使用");
        }

        return ResponseDTO.ok();

    }
}
